﻿// --------------------------------
// <copyright file="Checkbox.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>7. 12. 2011 18:54:01</date>

// <summary>
//
// </summary>
// --------------------------------
#if WINDOWS

using Nuclex.Input;
using ExternalClasses.Controls;
using System.ComponentModel;
using FITClub.TextureMenu.Configuration;
using System.Drawing.Design;

#endif

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Serialization;
using Microsoft.Xna.Framework.Input;
using FITClub.Util;

namespace FITClub.TextureMenu
{
    /// <summary>
    /// Checkbox with label.
    /// </summary>
#if WINDOWS

    [System.Serializable]
#endif
    public class Checkbox : AMenuItem, IUpdateableItem
    {
        #region Public properties

        [XmlAttribute]
#if WINDOWS
        [Category("Content")]
#endif
        public bool IsChecked
        {
            get { return ischecked; }
            set
            {
                if (value != ischecked)
                {
                    ischecked = value;
                    if (this.CheckedChanged != null)
                    {
                        this.CheckedChanged(this);
                    }
                }
            }
        }

        [XmlElement]
#if WINDOWS
        [Category("Content")]
#endif
        public string Text { get; set; }

        [XmlElement]
#if WINDOWS
        [Category("Colors")]
        [Editor(typeof(ColorEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public Color TextColor { get; set; }

        [XmlElement]
#if WINDOWS
        [Category("Path")]
        [Editor(typeof(FITClub.Episode1.Levels.ContentPathEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public string FontPath { get; set; }

        #endregion Public properties

        public event Action<Checkbox> CheckedChanged;

        #region Private properties
        [XmlIgnore]
        private bool ischecked;

        [XmlIgnore]
#if WINDOWS
        [NonSerialized]
#endif
        private Texture2D TextureChecked;

        [XmlIgnore]
#if WINDOWS
        [NonSerialized]
#endif
        private Texture2D TextureUnchecked;

#if WINDOWS
        [NonSerialized]
#endif
        private SpriteFont font;

        #endregion Private properties

        public override void Draw(SpriteBatch spriteBatch, Color tint)
        {
            base.Draw(spriteBatch, tint);
            Rectangle rect = new Rectangle(Bounds.X, Bounds.Y, Bounds.Height, Bounds.Height);

            if (this.IsChecked)
            {
                spriteBatch.Draw(this.TextureChecked, rect, tint);
            }
            else
            {
                spriteBatch.Draw(this.TextureUnchecked, rect, tint);
            }
            Vector2 pos = this.font.CenterString(this.Text, this.Bounds, MenuManager.ScaleX, MenuManager.ScaleY);
            pos.X = this.Bounds.X + rect.Width + rect.Width / 5;
            spriteBatch.DrawString(font, this.Text, pos, this.TextColor, 0, Vector2.Zero, (float)MenuManager.ScaleY, SpriteEffects.None, 0);
        }

        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager loader)
        {
            if (this.ContentLoaded == false)
            {
                this.ContentLoaded = true;
                this.font = MenuHelper.LoadFontOrDefault(loader, this.FontPath);
                this.TextureUnchecked = loader.Load<Texture2D>("menu\\checkbox_unchecked");
                this.TextureChecked = loader.Load<Texture2D>("menu\\checkbox_checked");
                if (this.Action == null || this.Action.Target == null || this.Action.Target.Trim() == string.Empty)
                {
                    this.Action = new MenuItemAction() { Target = "this", Method = "CheckAction" };
                }
                
            }
        }

        public override void Dispose()
        {
            if (this.ContentLoaded)
            {
                this.TextureChecked.Dispose();
                this.TextureUnchecked.Dispose();
                this.ContentLoaded = false;
            }
        }

        /// <summary>
        /// Changes check state.
        /// Usually invoked in many actions.
        /// </summary>
        public void CheckAction()
        {
            this.IsChecked = !this.IsChecked;            
        }

        public void Update(GameTime gt)
        {
            if (MenuManager.Current.HitTest(Keys.Space, 61) || MenuManager.Current.HitTest(Buttons.BigButton, 61)
#if WINDOWS
 || MenuManager.Current.HitTest(PCGamePadButtons.Attack, 61)
#endif
)
            {
                bool nw = !this.IsChecked;
                this.Owner.ActionPerformed();
                this.IsChecked = nw;
                //this.Action.Param = this.IsChecked.ToString();
            }
        }
    }
}